<?php
class ModelAccountAddress extends Model {
	public function addAddress($data) {
		$this->db->query("INSERT INTO " . DB_PREFIX . "address SET customer_id = '" . (int)$this->customer->getId() . "', company = '" . $this->db->escape($data['company']) . "', firstname = '" . $this->db->escape($data['firstname']) . "', lastname = '" . $this->db->escape($data['lastname']) . "', address_1 = '" . $this->db->escape($data['address_1']) . "', address_2 = '" . $this->db->escape($data['address_2']) . "', postcode = '" . $this->db->escape($data['postcode']) . "', city = '" . $this->db->escape($data['city']) . "', zone_id = '" . (int)$data['zone_id'] . "', country_id = '" . (int)$data['country_id'] . "'");
		
		$address_id = $this->db->getLastId();
		
		if (isset($data['default']) && $data['default'] == '1') {
			$this->db->query("UPDATE " . DB_PREFIX . "customer SET address_id = '" . (int)$address_id . "' WHERE customer_id = '" . (int)$this->customer->getId() . "'");
		}
		
		return $address_id;
	}
	
	public function editAddress($id, $data) {
		$this->db->query("UPDATE " . DB_PREFIX . "crm_customer SET first_name = '" . $this->db->escape($data['firstname']) . "', last_name = '" . $this->db->escape($data['lastname']) . "', alt_address_street = '" . $this->db->escape($data['address_1']) . "', primary_address_street = '" . $this->db->escape($data['address_2']) . "', primary_address_city = '" . $this->db->escape($data['city']) . "' WHERE id  = '" . $id . "'");
	
		if (isset($data['default']) && $data['default'] == '1') {
			$this->db->query("UPDATE " . DB_PREFIX . "customer SET address_id = '" . (int)$address_id . "' WHERE customer_id = '" . (int)$this->customer->getId() . "'");
		}
	}
		
	
	public function getAddress($id) {
		$address_data = array();
		
		 $results  = $this->db->query("SELECT * FROM" . DB_PREFIX . " crm_customer WHERE id = '" . $id . "'"); 
		 $result = $results->rows;
			$address_data[] = array(
                                'id'             => $result[0]['id'],
				'firstname'      => $result[0]['first_name'],
				'lastname'       => $result[0]['last_name'],
				//'company'        => $result[0]['company'],
				'address_1'      => $result[0]['alt_address_street'],
				'address_2'      => $result[0]['primary_address_street'],
				//'postcode'       => $result[''],
				'city'           => $result[0]['primary_address_city'],
				//'zone_id'        => $result['zone_id'],
				//'zone'           => $zone,
			//	'zone_code'      => $code,
			//	'country_id'     => $result['primary_address_country'],
				'country'        => $result[0]['primary_address_country'],	
				//'iso_code_2'     => $iso_code_2,
				//'iso_code_3'     => $iso_code_3,
				'address_format' => ''
			);
				
		
		return $address_data;
	}
	
	public function getAddresses() {
		$address_data = array();
		
		 $results  = $this->db->query("SELECT * FROM" . DB_PREFIX . " crm_customer WHERE id = '" . $this->customer->getId() . "'");  		
		 $result = $results->rows;
			$address_data[] = array(
                                'id'             => $result[0]['id'],
				'firstname'      => $result[0]['first_name'],
				'lastname'       => $result[0]['last_name'],
				//'company'        => $result[0]['company'],
				'address_1'      => $result[0]['alt_address_street'],
				'address_2'      => $result[0]['primary_address_street'],
				//'postcode'       => $result[''],
				'city'           => $result[0]['primary_address_city'],
				//'zone_id'        => $result['zone_id'],
				//'zone'           => $zone,
			//	'zone_code'      => $code,
			//	'country_id'     => $result['primary_address_country'],
				'country'        => $result[0]['primary_address_country'],	
				//'iso_code_2'     => $iso_code_2,
				//'iso_code_3'     => $iso_code_3,
				'address_format' => ''
			);
				
		
		return $address_data;
	}	
	
	public function getTotalAddresses() {
		$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "address WHERE customer_id = '" . (int)$this->customer->getId() . "'");
	
		return $query->row['total'];
	}
}
?>